<template>
	<!-- 设备信息 -->
	<view>
		<view class="card">
			<view class="">设备名称：{{deviceInfo.name}}</view>
			<view class="m-t-20">设备地址： {{deviceInfo.deviceId}}</view>
			<view class="m-t-20">设备信息： {{deviceInfo.remark || "无"}}</view>
		</view>
		<view class="card">
			<view class="u-flex u-row-between" @click="showPop = true">
				<view class="f-blod">修改设备名称</view>
				<image src="../../static/arrow-right.png" class="arrow"></image>
			</view>
		</view>
		<view class="card">
			<view class="u-flex u-row-between" @click="showPop2 = true">
				<view class="f-blod">修改设备信息</view>
				<image src="../../static/arrow-right.png" class="arrow"></image>
			</view>
		</view>
		<view class="card">
			<view class="u-flex u-row-between" @click="showFirmwareInfo = !showFirmwareInfo">
				<view class="f-blod">读取设备固件信息</view>
				<image src="../../static/arrow-right.png" class="arrow"></image>
			</view>
			<view class="c-grey m-t-20" v-show="showFirmwareInfo">
				设备固件信息
			</view>
		</view>

		<u-modal :show="showPop" showCancelButton title="修改设备名称" @cancel="showPop = false" @confirm="handleSave">
			<view class="">
				<input class="input-box" type="text" v-model="deviceName" placeholder="请输入设备名称" />
			</view>
		</u-modal>
		<u-modal :show="showPop2" showCancelButton title="修改设备信息" @cancel="showPop2 = false" @confirm="handleSaveDeviceRemark">
			<view class="">
				<input class="input-box" type="text" v-model="deviceRemark" placeholder="请输入设备信息" />
			</view>
		</u-modal>
	</view>
</template>

<script>
	import { getUuid, writeBLECharacteristicValue } from "../../utils/common";
	import {
		getDevice,
		setDevice
	} from "../../utils/storage";
	const app = getApp();
	export default {
		data() {
			return {
				showFirmwareInfo: false,
				showPop: false,
				showPop2:false,
				deviceName: "",
				deviceRemark:"",
				deviceInfo:{}
			};
		},
		onLoad() {
			const deviceList = getDevice();
			const currentDeviceList = deviceList.filter((item) => item.deviceId == app.globalData
				.deviceId);
			this.deviceInfo = currentDeviceList[0];
			console.log('currentDeviceList: ',currentDeviceList);
		},
		methods: {
			handleSaveDeviceRemark(){
				if(!this.deviceRemark){
					uni.$u.toast("请输入设备信息");
					return
				}
				let deviceList = getDevice();
				for (var i = 0; i < deviceList.length; i++) {
					if(deviceList[i].deviceId == this.deviceInfo.deviceId){
						deviceList[i].remark = this.deviceRemark;
						this.deviceInfo.remark = this.deviceRemark;
					}
				}
				setDevice(deviceList);
				this.showPop2 = false;
			},
			handleSave() {
				if(!this.deviceName){
					uni.$u.toast("请输入设备名称");
					return
				}
				let deviceList = getDevice();
				for (var i = 0; i < deviceList.length; i++) {
					if(deviceList[i].deviceId == this.deviceInfo.deviceId){
						deviceList[i].name = this.deviceName;
						this.deviceInfo.name = this.deviceName;
					}
				}
				setDevice(deviceList);
				const serviceId = getUuid("5508");
				writeBLECharacteristicValue(serviceId, getUuid("0050"), this.deviceName, "设备名称修改");
				this.showPop = false;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.card {
		background-color: white;
		padding: 30rpx;
		border-radius: 10rpx;
		box-shadow: 0px 3px 8px 0px rgba(0, 0, 0, 0.16);
		margin: 30rpx 40rpx 0 40rpx;
	}

	.arrow {
		transform: rotate(90deg);
		width: 35rpx;
		height: 35rpx;
	}

	.input-box {
		border-radius: 10rpx;
		border: 1px solid #e6e6e6;
		padding: 15rpx 20rpx;
		width: 450rpx;
	}
</style>